<%@page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@include file="/common/session.jsp" %>
<c:set var="openid" value="${param.openid}"/>
<c:set var="accessToken" value="${param.accessToken}"/>
<HTML>
<HEAD>
    <TITLE>login</TITLE>
    <META http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <META name="viewport" content="initial-scale=1.0, minimum-scale=1.0, maximum-scale=2.0, user-scalable=no, width=device-width">
    <META content="yes" name="apple-mobile-web-app-capable">
    <META content="black" name="apple-mobile-web-app-status-bar-style">
    <link rel="stylesheet" href="${ctx}/wx/css/default.css"/>
    <script type="text/javascript" src="${ctx}/dwr/engine.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/util.js"></script>
    <script type="text/javascript" src="${ctx}/dwr/interface/WxService.js"></script>
    <script type="text/javascript" src="${ctx}/js/loading.js"></script>
</HEAD>
<body bgcolor="#DEEEBD">
<form>
    openid:<input type="text" id="openid" name="openid" value="${openid}" readonly>
    <br/>
    accessToken:<input type="text" id="accessToken" name="accessToken" value="${accessToken}" readonly><br>
    money:<input type="number" step="0.01" id="money" name="money" value="0.01" readonly><br>
    outTradeNo:<input type="text" id="outTradeNo" name="outTradeNo" value="100000000002">
    <br/>
    <button type="button" onclick="doUnifiedOrder()">Pay</button>
</form>
</body>
</html>
<script type="text/javascript">

    var appId = "";
    var timeStamp = "";
    var nonceStr = "";
    var package = "";
    var signType = "";
    var paySign = "";

    dwr.util.setValue("outTradeNo", new Date().Format("yyMMddhhmmss"));

    function doUnifiedOrder(money) {
        var openid = dwr.util.getValue("openid");
        var money = dwr.util.getValue("money");
        var outTradeNo = dwr.util.getValue("outTradeNo");
        WxService.doUnifiedOrder("学费", outTradeNo, money, openid, function (result) {
            alert("appId=" + result.appId + "\r\n" +
                "timeStamp=" + result.timeStamp + "\r\n" +
                "nonceStr=" + result.nonceStr + "\r\n" +
                "package=" + result.packageStr + "\r\n" +
                "signType=" + result.signType + "\r\n" +
                "paySign=" + result.paySign);
            appId = result.appId;
            timeStamp = result.timeStamp;
            nonceStr = result.nonceStr;
            package = result.packageStr;
            signType = result.signType;
            paySign = result.paySign;

            if (typeof WeixinJSBridge == "undefined") {
                if (document.addEventListener) {
                    document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
                } else if (document.attachEvent) {
                    document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
                    document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
                }
            } else {
                onBridgeReady();
            }
        });
    }

    function onBridgeReady() {
        WeixinJSBridge.invoke('getBrandWCPayRequest', {
                "appId": appId,     //公众号名称,由商户传入
                "timeStamp": timeStamp,         //时间戳,自1970年以来的秒数
                "nonceStr": nonceStr, //随机串
                "package": package,
                "signType": signType,         //微信签名方式：
                "paySign": paySign //微信签名
            },
            function (res) {
                if (res.err_msg == "get_brand_wcpay_request:ok") {
                    console.log('支付成功');
                    //支付成功后跳转的页面
                } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                    console.log('支付取消');
                } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                    console.log('支付失败');
                    WeixinJSBridge.call('closeWindow');
                } //使用以上方式判断前端返回,微信团队郑重提示：res.err_msg将在用户支付成功后返回ok,但并不保证它绝对可靠。
            }
        );
    }
</script>
